package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.demo.utils.ResponseResult;
import com.example.dto.UserDto;
import com.example.jwt.JwtUtil;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
    @Autowired
    UserService userService;

    @ResponseBody
    @PostMapping(value = "toLogin")
    public ResponseResult login(String username, String password) {
        ResponseResult<JSONObject> result = new ResponseResult<>();
        JSONObject json = new JSONObject();

        UserDto user = userService.getUserInfo(username);
        if (user == null) {
            json.put("error", "用户不存在");
            result.setData(json);
            return result;
        }

        /*if (!user.getPassword().equals(password)) {
            json.put("error", "密码不正确");
            result.setData(json);
            return result;
        }*/

        String token = JwtUtil.sign(username, password);
        json.put("token", token);
        result.setData(json);
        return result;
    }

}
